package com.controller;

import com.domain.Admin;
import com.domain.BlogArticle;
import com.domain.BlogComment;
import com.domain.StaticticBo;
import com.mapper.AdminMapper;
import com.service.SiteService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    SiteService siteService;
    @Autowired
    AdminMapper adminMapper;

    @GetMapping("/login")
    public String toLoginPage() {
        return "back/login";
    }
//        @GetMapping("")
//          public String index(){
//    return "back/index"1;
// }
   @PostMapping("/toLogin")
    public String login(Admin admin, HttpSession httpSession, Model model) {
        Admin admin1 = adminMapper.userLogin(admin);
        if (admin1 != null){
            httpSession.setAttribute("admin",admin1);
            model.addAttribute("admin",admin1);
            return "redirect:/admin";
        }
        else{
           model.addAttribute("msg","账号或密码错误，请重新输入！");
           return "back/login";
        }
   }
   @GetMapping("")
    public String index(HttpServletRequest request) {
        List<BlogArticle> articles = siteService.recentArticles(5);
        List<BlogComment> comments = siteService.recentComments(5);
        StaticticBo staticticBo = siteService.getStatistics();
        request.setAttribute("comments",comments);
       request.setAttribute("articles",articles);
       request.setAttribute("statistics",staticticBo);
       return "back/index";
   }

   @GetMapping(value = "ue")
    public String ue(Model model){return "index";}

}
